<?php 
class CMStudent extends MStudent{	
	static public function getNewSID($departmentID){
		$tahunAjaranAktif = CMTahunAjaran::getTahunAjaranActive($departmentID);		
		if ($tahunAjaranAktif == null){
			throw new Exception(Yii::t("label","Tidak ada tahun ajaran yang aktif."));
		}
		//$department = MDepartment::model()->findByPk($departmentID,"name=:name",array(":name"=>"KB"));
		$department = MDepartment::model()->findByPk($departmentID);
		if ($department == null){
			throw new Exception(Yii::t("label","Departement tidak di kenal."));
		}
		$sql = "select * from m_student where department_id =:depId and tahun_ajaran_id = :tahunAktifId order by sid desc limit 1";
		$lastStudent = MStudent::model()->findBySql($sql,array(":depId"=>$departmentID,":tahunAktifId"=>$tahunAjaranAktif->id));
		if ($lastStudent != null){
			$arrTmp = explode("-", $lastStudent->sid);
			$lastNou = intval($arrTmp[count($arrTmp)-1]);			
		}else{
			$lastNou =0;
		}
		$newNou = $lastNou + 1;				
		$lengthSid = intval(AppSetting::getValue("akSIDAutoLength"));
		$strNewNou =  str_repeat("0", $lengthSid) . $newNou;
		$strNewNou =  substr($strNewNou,strlen($strNewNou) - $lengthSid);
		return $department->sid_prefix . $tahunAjaranAktif->sid_prefix . "-". $strNewNou;
	}
	
	static public function getNewTID($departmentID){
		$tahunAjaranAktif = CMTahunAjaran::getTahunAjaranActive($departmentID);
		if ($tahunAjaranAktif == null){
			throw new Exception(Yii::t("label","Tidak ada tahun ajaran yang aktif."));
		}
		//$department = MDepartment::model()->findByPk($departmentID,"name=:name",array(":name"=>"KB"));
		$department = MDepartment::model()->findByPk($departmentID);
		if ($department == null){
			throw new Exception(Yii::t("label","Departement tidak di kenal."));
		}
		$sql = "select * from m_student where department_id =:depId and tahun_ajaran_id = :tahunAktifId and trial_id is not null and trial_id != '' order by trial_id desc limit 1";
		$lastStudent = MStudent::model()->findBySql($sql,array(":depId"=>$departmentID,":tahunAktifId"=>$tahunAjaranAktif->id));
		if ($lastStudent != null){
			$arrTmp = explode("-", $lastStudent->trial_id);
			$lastNou = intval($arrTmp[count($arrTmp)-1]);
		}else{
			$lastNou =0;
		}
		$newNou = $lastNou + 1;
		
		$lengthTid = intval(AppSetting::getValue("akTIDAutoLength"));
		$strNewNou =  str_repeat("0", $lengthTid) . $newNou;
		$strNewNou =  substr($strNewNou,strlen($strNewNou) - $lengthTid);		
		return AppSetting::getValue("akTIDPrefix") . $department->sid_prefix . $tahunAjaranAktif->sid_prefix . "-". $strNewNou;
	}
	
	static public function getActiveClass($tahunAjaranId="",$ar){
		
		$studentId="";
		if ($ar instanceof MStudent) {
			$studentId = $ar->id;			
			if ($tahunAjaranId == ""){
				$tahunAjaran = CMTahunAjaran::getTahunAjaranActive($ar->department_id);
				$tahunAjaranId = $tahunAjaran->id;
			}
		}else{
			//ar is sudent id;
			$studentId = $ar;
		}
		//if ($tahunAjaran != null){
			$sql = "select b.tingkatan_id,b.tingkatan_name,b.kelas_id,b.kelas_name from m_student_class a inner join v_kelas_tahun_ajaran b on a.kelas_tahun_ajaran_id = b.id where 
				a.student_id = :studentId and b.tahun_ajaran_id = :tahunAjaranId and a.is_active = true";
			$conn = Yii::app()->db;
			$cmd = $conn->createCommand($sql);
			$rs = $cmd->query(array(":studentId"=>$studentId,":tahunAjaranId"=>$tahunAjaranId));
			$rw = $rs->read();
			return $rw;
		//}else{
		//	return null;
		//}		
	}
	
	static public function getFamilyAjx($id){
		$family = MStudentFamily::model()->findAll("Student_id =:studentId",array(":studentId"=>$id));
		return $family;
	}
	
}
?>